package com.lowcodeai.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lowcodeai.common.result.Result;
import com.lowcodeai.user.dto.UserCreateRequest;
import com.lowcodeai.user.dto.UserQueryRequest;
import com.lowcodeai.user.dto.UserUpdateRequest;
import com.lowcodeai.user.entity.UserInfo;
import com.lowcodeai.user.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 用户管理控制器
 * 
 * @author LowCodeAI
 * @since 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Tag(name = "用户管理", description = "用户管理相关接口")
@Validated
public class UserController {

    private final UserService userService;

    @PostMapping
    @Operation(summary = "创建用户", description = "创建新用户")
    public Result<UserInfo> createUser(@Valid @RequestBody UserCreateRequest request) {
        return userService.createUser(request);
    }

    @PutMapping
    @Operation(summary = "更新用户", description = "更新用户信息")
    public Result<UserInfo> updateUser(@Valid @RequestBody UserUpdateRequest request) {
        return userService.updateUser(request);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除用户", description = "根据ID删除用户")
    public Result<Void> deleteUser(@PathVariable @NotNull Long id) {
        return userService.deleteUser(id);
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除用户", description = "批量删除用户")
    public Result<Void> deleteUsers(@RequestBody List<Long> ids) {
        return userService.deleteUsers(ids);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取用户详情", description = "根据ID获取用户详情")
    public Result<UserInfo> getUserById(@PathVariable @NotNull Long id) {
        return userService.getUserById(id);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询用户", description = "分页查询用户列表")
    public Result<IPage<UserInfo>> getUserPage(@Valid UserQueryRequest request) {
        return userService.getUserPage(request);
    }

    @PostMapping("/{id}/reset-password")
    @Operation(summary = "重置密码", description = "重置用户密码")
    public Result<Void> resetPassword(@PathVariable @NotNull Long id, 
                                    @RequestParam @NotBlank String newPassword) {
        return userService.resetPassword(id, newPassword);
    }

    @PostMapping("/{id}/status")
    @Operation(summary = "更新用户状态", description = "启用或禁用用户")
    public Result<Void> updateUserStatus(@PathVariable @NotNull Long id, 
                                       @RequestParam @NotNull Integer status) {
        return userService.updateUserStatus(id, status);
    }

    @GetMapping("/check/username")
    @Operation(summary = "检查用户名是否存在", description = "检查用户名是否已被使用")
    public Result<Boolean> checkUsernameExists(@RequestParam @NotBlank String username) {
        return userService.checkUsernameExists(username);
    }

    @GetMapping("/check/email")
    @Operation(summary = "检查邮箱是否存在", description = "检查邮箱是否已被使用")
    public Result<Boolean> checkEmailExists(@RequestParam String email) {
        return userService.checkEmailExists(email);
    }

    @GetMapping("/check/phone")
    @Operation(summary = "检查手机号是否存在", description = "检查手机号是否已被使用")
    public Result<Boolean> checkPhoneExists(@RequestParam String phone) {
        return userService.checkPhoneExists(phone);
    }
} 